package com.bwhx.eruler.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bwhx.eruler.bean.Buyer;
import com.bwhx.eruler.bean.EChartsResult;
import com.bwhx.eruler.bean.OfflineOrder;
import com.bwhx.eruler.bean.Order;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.dao.BuyerDao;
import com.bwhx.eruler.dao.OfflineOrderDao;
import com.bwhx.eruler.dao.OrderDao;
import com.bwhx.eruler.dao.RulerDao;
import com.bwhx.eruler.dao.StaffDao;
import com.bwhx.eruler.service.OfflineOrderService;
import com.bwhx.eruler.util.StringUtils;

/**
 * @Description:
 */
@Service
public class OfflineOrderServiceImpl implements OfflineOrderService {
	@Autowired 
	OfflineOrderDao orderDao;
	@Autowired
	StaffDao staffDao;
	@Autowired
	RulerDao rulerDao;
	@Autowired
	BuyerDao buyerDao;

	

	@Override
	public List<OfflineOrder> list(Page p) {
		return orderDao.list(p);
	}



	@Override
	public Integer count(OfflineOrder oo) {
		return orderDao.count(oo);
	}



	@Override
	public void insert(OfflineOrder order) {
		
		Staff kuguan = staffDao.findById(order.getoOKuGuanId());
		order.setoOKuGuanName(kuguan.getsName());
		Staff receiver = staffDao.findById(order.getoOReceiverId());
		order.setoOReceiverName(receiver.getsName());
		if(order.getoOBuyerId()!=null&&order.getoOBuyerId()!=0){
			Buyer buyer = buyerDao.get(order.getoOBuyerId());
			order.setoOBuyerName(buyer.getbName());
		}
		

		orderDao.insert(order);
		
		
		
	}



	@Override
	public OfflineOrder get(String oId) {
		return  orderDao.get(oId);
	}



	@Transactional
	@Override
	public void update(OfflineOrder oOrder, List<Ruler> rulers) {
		rulerDao.updateByOrder(rulers);
		orderDao.update(oOrder);
		Integer bSum0Tape=0;
		Integer bSum1Tape=0;
		Integer bSum2Tape=0;
		Integer bSum3Tape=0; 
		for (Ruler ruler : rulers) {
			if(ruler.getrVersion()==0){
				bSum0Tape+=1;
			}else if(ruler.getrVersion()==1){
				bSum1Tape+=1;
			}else if(ruler.getrVersion()==2){
				bSum2Tape+=1;
			}else if(ruler.getrVersion()==3){
				bSum3Tape+=1;
			}
			
		}
		if(oOrder.getoOBuyerId()!=null&&oOrder.getoOBuyerId()!=0){
			HashMap<String,Integer> map = new HashMap<String, Integer>();
			map.put("id", oOrder.getoOBuyerId());
			map.put("number",oOrder.getoOPNumber());
			map.put("number0", bSum0Tape);
			map.put("number1", bSum1Tape);
			map.put("number2", bSum2Tape);
			map.put("number3", bSum3Tape);
			buyerDao.update(map);
		}
		
		
	}



	@Override
	public void del(String oOid) {
		orderDao.delete(oOid);
		
	}



	@Override
	public List<EChartsResult> countRuler(String year) {
		return orderDao.countRuler(year);
	}



	@Override
	public List<EChartsResult> countSeller() {
		
		return orderDao.countSeller();
	}



}
